package test1;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.CheckedOutputStream;

import check.md5.*;

public class test7 {

	public void main() throws NoSuchAlgorithmException, IOException {
		ObjectToWriteRead obj = new ObjectToWriteRead();
		obj.setA(10);
		obj.setB(1.0f);

		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("obj.xml"));
		CheckedSumOutputStream checkedOutputStream = new CheckedSumOutputStream(bufferedOutputStream,
				MessageDigest.getInstance("MD5"));

		XMLEncoder encoder = new XMLEncoder(bufferedOutputStream);
		encoder.writeObject(obj);
		encoder.close();

		// ============= Liczenie sumy kontrolnej ======
		checkedOutputStream.getMessageDigest().update("a".getBytes());
		byte[] dupa = "a".getBytes("UTF8");
		byte[] aMessageDigest = checkedOutputStream.getMessageDigest().digest();
		StringBuffer sb = new StringBuffer();
		for (int j = 0; j < aMessageDigest.length; j++) {
			sb.append(Integer.toString((aMessageDigest[j] & 0xff) + 0x100, 16).substring(1));
		}
		System.out.println(new String(sb).toUpperCase());

		// PROBA 2
		bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("obj.xml"));
		CheckedOutputStream checkedOutputStream_simple = new CheckedOutputStream(bufferedOutputStream, new MessageDigestAdapterImpl(
				MessageDigest.getInstance("MD5")));

		encoder = new XMLEncoder(bufferedOutputStream);
		encoder.writeObject(obj);
		encoder.close();
		
		MessageDigestAdapterImpl adapterImpl = (MessageDigestAdapterImpl) checkedOutputStream_simple.getChecksum();
		adapterImpl.digest("a");
		System.out.println(adapterImpl.getHexString());
		// //============= Odczytywanie =================
		// // Pomocnicze strumienie
		// BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("obj.xml"));
		// CheckedSumInputStream checkedInputStream = new CheckedSumInputStream(bufferedInputStream,
		// MessageDigest.getInstance("MD5"));
		//
		// // Odczytanie XML-a
		// XMLDecoder decoder = new XMLDecoder(checkedInputStream);
		// ObjectToWriteRead o = (ObjectToWriteRead) decoder.readObject();
		// decoder.close();
		//
		// byte[] aMessageDigest= checkedInputStream.getMessageDigest().digest("dupa.8".getBytes());
		// sb = new StringBuffer();
		// for (int j = 0; j < aMessageDigest.length; j++) {
		// sb.append(Integer.toString((aMessageDigest[j] & 0xff) + 0x100, 16).substring(1));
		// }
		// System.out.println(new String(sb).toUpperCase());

		/*
		 * RandomAccessFile randomAccessFile = new RandomAccessFile("obj2.xml", "rw");
		 * randomAccessFile.seek(randomAccessFile.length()); randomAccessFile.write("a".getBytes());
		 * randomAccessFile.close();
		 */
	}
}